package com.wyp168.juc;

/**
 * @ClassName InterruptedDemo001
 * @Description 学习Thread类中的Interrupted()方法的使用
 * @Author wyp168
 * @Date 2022/7/8 14:22
 */
public class InterruptedDemo001 {
    public static void main(String[] args) throws InterruptedException {
        Thread threadA = new Thread(() -> {
            while (!Thread.currentThread().isInterrupted()) {
                System.out.println(Thread.currentThread() + " is running...");
            }
            System.out.println(Thread.currentThread() + " has done!");
        });

        Thread thread = Thread.currentThread();


        //启动threadA线程
        threadA.start();

        //让主线程睡眠1秒中，使得子线程发挥
        Thread.currentThread().sleep(1000);

        //中断子线程
        System.out.println("Main thread interrupt threadA...");
        threadA.interrupt();

        //等待threadA线程执行结束
        threadA.join();
        System.out.println("All child thread is over!");
    }
}
